﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HuaCheng.DataAccess
{
    public class StepSchemeHelper
    {
        /// <summary>
        /// 工步方案表中新增工步方案记录
        /// </summary>
        /// <param name="bts"></param>
        public static void InsertNewStepScheme(StepScheme stepScheme)
        {
            string sql = "insert into step_scheme(test_id,dev_ip,cabinet_id,mid_id,chl_id,step_data,standard_flag,insert_time)" +
                " values (@testid,@devip,@gid,@mid,@chlid,@stepdata,@standardflag,@inserttime)";
            try
            {
                using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
                {
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@testid", stepScheme.TestId));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@devip", stepScheme.DevIp));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@gid", stepScheme.GID));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@mid", stepScheme.MID));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@chlid", stepScheme.ChlId));               
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@stepdata", stepScheme.StepSchemeData));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@standardflag", stepScheme.StandardFlag));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@inserttime", stepScheme.InsertTime));
                    DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
                }
            }
            catch (Exception ex) { Console.WriteLine(ex.Message + ex.StackTrace); }
        }
        /// <summary>
        /// 查询某次测试中设置的所有工步方案
        /// </summary>
        /// <param name="testId"></param>
        /// <param name="ip"></param>
        /// <param name="uid"></param>
        /// <param name="cid"></param>
        /// <returns></returns>
        public static List<byte[]> RetriveStepScheme(string testId, string ip, int gid,int mid, int cid)
        {
            List<byte[]> biList = new List<byte[]>();
            string sql = "select step_data from step_scheme where test_id=@testid and dev_ip=@devip and cabinet_id=@gid and mid_id=@mid and chl_id=@chlid";
            try
            {
                DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
                cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@testid", testId));
                cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@devip", ip));
                cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@gid", gid));
                cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@mid", mid));
                cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@chlid", cid));
                using(DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
                {
                    if(reader.HasRows){
                        while (reader.Read())
                        {
                            
                            byte[] stepData = new byte[reader.GetBytes(0, 0, null, 0, int.MaxValue)];
                            reader.GetBytes(0, 0, stepData, 0, stepData.Length);
                            biList.Add(stepData);
                        }
                    }

                }
            }
            catch (Exception ex) { Console.WriteLine(ex.Message + ex.StackTrace); }
            return biList;
        }
    }
}
